package com.cskaoyan.market.admin.service.impl;

import com.cskaoyan.market.admin.service.MarketAdminCommentService;
import com.cskaoyan.market.db.domain.MarketComment;
import com.cskaoyan.market.db.domain.MarketCommentExample;
import com.cskaoyan.market.db.mapper.MarketCommentMapper;
import com.cskaoyan.market.util.MyBatisUtil;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author James
 * @since 2023/03/21 12:08
 */

public class MarketAdminCommentServiceImpl implements MarketAdminCommentService {
    @Override
    public List<MarketComment> list(int page, int limit, String userId, int valueId, String sort, String order) {
        SqlSession session = MyBatisUtil.getSession();
        MarketCommentMapper commentMapper = session.getMapper(MarketCommentMapper.class);
        MarketCommentExample commentExample = new MarketCommentExample();
        MarketCommentExample.Criteria criteria = commentExample.createCriteria();
        //构建查询条件
        if (valueId != 0) {
            criteria.andValueIdEqualTo(valueId);
        }
        if (!StringUtils.isEmpty(userId)) {
            criteria.andUserIdEqualTo(Integer.valueOf(userId));
        }
        criteria.andDeletedEqualTo(false);
        //设置排序规则
        commentExample.setOrderByClause(sort + " " + order);
        PageHelper.startPage(page, limit);
        //select * from tb where xxx order by add_time desc
        List<MarketComment> marketComment = commentMapper.selectByExample(commentExample);
        session.commit();
        session.close();
        return marketComment;
    }

    @Override
    public void delete(String addTime, String content, String id, String updateTime, String star, String valueId) {
        SqlSession session = MyBatisUtil.getSession();
        MarketCommentMapper commentMapper = session.getMapper(MarketCommentMapper.class);
        MarketComment marketComment = commentMapper.selectByPrimaryKey(Integer.parseInt(id));
        marketComment.setUpdateTime(LocalDateTime.now());
        marketComment.andLogicalDeleted(true);
        marketComment.setDeleted(true);
        commentMapper.updateByPrimaryKey(marketComment);
        session.commit();
        session.close();

    }
}
